package com.briup.estore.web.servlet;

import com.briup.estore.bean.Customer;
import com.briup.estore.service.CustomerService;
import com.briup.estore.service.Impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/userRegister")
public class RegisterServlet extends HttpServlet {
    private CustomerService customerService;
    {
        customerService =new CustomerServiceImpl();
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("name");
        String password = req.getParameter("password");
        String zipCode = req.getParameter("zipCode");
        String address = req.getParameter("address");
        String telephone = req.getParameter("telephone");
        String email = req.getParameter("email");
        //拼成customer对象。传递给service层
        Customer customer = new Customer();
        customer.setName(username);
        customer.setPassword(password);
        customer.setZipcode(zipCode);
        customer.setAddress(address);
        customer.setTelephone(telephone);
        customer.setEmail(email);
        customerService.register(customer);
        //注册成功之后。不做跳转
        //直接登陆到主页
        HttpSession session =req.getSession();
        session.setAttribute("msg","注册成功，即将返回登录");
        resp.sendRedirect("login");
    }
}
